import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/up-form.vue"; //弹框form

import {
    saveyjQmpGySgphb, //保存
    getSGPHBByFid, //详情
    // getBaseDataitemDetailByItemCode, //数据字典
    getCFDdByEntryledger, //位置
    getyjQmpGyLlphbPageLists,
    getLLPHBByFids,



} from "@/api/request.js";

export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "fCompanyname",
        "modelType"
    ],
    data() {
        return {
            activeNames: ["0", "1", "2"],
            fileList: [],
            isEdit: true,
            isEdits: true,

            handNum: false, //闭合申请单
            firstNum: false, //一级审核
            secondNum: false, //二级审核
            visible: "",
            title: null,
            isLock: false,
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj, //基础信息
            formData: { //闭合 closeFormObj
                fId: "", //传fid表示修改 不传表示新增
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
                fCompanyid: "", //标段id
                fCompanyname: "", //标段名称
                fProjectid: "", //项目id
                fProjectname: "", //项目名称
                fSgphbnumber: "", //施工配合比编号预新增提供
                fLlphbnumber: "", //理论配合比报告编号 
                fHnttype: "", //混凝土种类
                fHnttypeid: "",
                fQdlevel: "", //设计强度等级
                fSgsite: "", //混凝土施工部位
                fSgsiteid: "",
                fMixingmethod: "", //拌合及捣振方法
                fWatercement: "", //水胶比
                fDesignedslump: "", //设计塌落度 用~隔开
                fLlphbdates: "", //理论配合比报告日期
                fSgphbdates: "", //施工配合比报告日期
                fFileurl: "", //说明书及其他资料    预新增提供
                fReportid: "", //编制人id
                fReportname: "", //编制人姓名
                yjQmpGySgphbmaterials: [],
                fStatus: "", //状态
                fNote: ""
            }, //施工配合比原材料信息


            tableData: [],
            tablesgData: [],

            tableObj: [],
            LocationData: [],

            baseFormData: {},
            closeFormObj: formJs.closeFormObj, //闭合


            FauditFormObj: formJs.Faudit, //一级审核
            FauditFileurllist: [], //附件

            btnShow: true,
            currentDetail: null,


            fCompanynames: null,
            fCompanyids: null,
            modelTypes: null

        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        modelType: {
            handler(newVal) {
                if (newVal) {
                    this.modelTypes = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {


                    this.edit(newVal);
                }

            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    // fStatus

                    if (this.statusNum != 1 && this.statusNum != 8) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        }) // currentRow
                    }
                }
            },

            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {

                    this.fCompanyids = newVal

                }
            },
            immediate: true,
        },
        fCompanyname: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal

                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("monitorBack-userInfo"));
        this.btnShow = true
        this.yj_searchForm()
        // this.getBaseDataitemDetailByItemCode('Hnttype', 1) //混凝土种类(Hnttype)
        // this.getBaseDataitemDetailByItemCode('Qdlevel', 2) //设计强度等级(Qdlevel)
        // this.getBaseDataitemDetailByItemCode('Sgsite', 3) //混凝土施工部位(Sgsite)
        this.handNum = true; //闭合申请单
        this.firstNum = true; //一级审核


        if (this.statusNum == 1) { //新增
            this.getDetail({
                fId: "",
                fCompanyid: this.userInfo.companyId
            })
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[0].disabled = false;
            this.formObj[1].disabled = false;

            this.formObj[8].disabled = true;
            this.formObj[9].uploadSelect = true
            this.isEdits = false
        } else if (this.statusNum == 2) { //编辑
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[0].disabled = false;
            this.formObj[1].disabled = false;
            this.formObj[8].disabled = true;
            this.formObj[9].uploadSelect = true
            this.isEdits = false

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false

            // 闭合
            this.formObj.map((item) => {
                item.disabled = true;
            });

            this.isEdits = true

            this.formObj[9].uploadSelect = false
        } else if (this.statusNum == 8) { //查看
            this.btnShow = false
            this.getDetail({
                fId: this.currentDetail.fSgphbnumber
            }) // currentRow
            // 闭合
            this.formObj.map((item) => {
                item.disabled = true;
            });

            this.isEdits = true

            this.formObj[9].uploadSelect = false
        }



        this.getHeight();
    },
    methods: {
        // 折叠面板
        changeCollapse(e) {
            console.log(this.activeNames, e);
        },
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            if (b == "fLlphbnumber") {
                this.formData.fLlphbnumber = data.label; //理论配合比报告编号 
                this.formData.fHnttype = data.value.fHnttype //混凝土种类
                this.formData.fHnttypeid = data.value.fHnttypeid
                this.formData.fQdlevel = data.value.fQdlevel //设计强度等级
                this.formData.fSgsite = data.value.fSgsite //混凝土施工部位
                this.formData.fSgsiteid = data.value.fSgsiteid
                this.formData.fLlphbdates = data.value.fLlphbdates
                this.formData.fMixingmethod = data.value.fMixingmethod //拌合及捣振方法
                this.formData.fWatercement = data.value.fWatercement //水胶比
                this.formData.fDesignedslump = data.value.fDesignedslump.split(",") //设计塌落度 用~隔开

                this.getLLPHBByFids(data.value.fId)
                // this.formData.fHnttype = data.label;
                // this.formData.fHnttypeid = data.value
            }
            // else if (b == "fMixingmethod") { //拌和及振捣方法
            //     this.formData.fMixingmethod = data.label; //拌和及振捣方法
            // } else if (b == 'fSgsite') {
            //     //混凝土施工部位
            //     this.formData.fSgsite = data.label
            //     this.formData.fSgsiteid = data.value
            // } else if (b == 'fQdlevel') { //设计强度
            //     this.formData.fQdlevel = data.label
            // }
        },
        edit() {
            this.closeFormData = this.$options.data.call(this).closeFormData;
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fileList.length; index++) {
                if (file.uid == this.fileList[index].uid) {
                    if (this.fileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].atta.fId,
                        });
                    }
                    this.fileList.splice(index, 1);
                }
            }
        },


        // 位置选择
        selectLocation(item, index) { //存放位置选择
            this.tableData[index].fLocationname = item.F_LocationName //地点名称
            this.tableData[index].fLocationid = item.F_LocationId //地点名称
            this.tableData[index].fLocationnum = item.F_LocationNum //数量
            this.tableData[index].fPznumber = item.F_ReportNumber //报告编号
            console.log(item);
            // this.formData.yjQmpGySgphbmaterials[index].fLocationname = item.F_LocationName //地点名称
            // this.formData.yjQmpGySgphbmaterials[index].fLocationid = item.F_LocationId //地点名称
            // this.formData.yjQmpGySgphbmaterials[index].fLocationnum = item.F_LocationNum //数量
            // this.formData.yjQmpGySgphbmaterials[index].fPznumber = item.F_ReportNumber //报告编号
            this.$forceUpdate()
        },

        // 位置
        getCFDdByEntryledger(data, index) {
            var obj = {
                fCompanyid: this.userInfo.companyId, // 左侧选中组织机构id
                fMaterialtypeid: data.fMaterialtypeid, //材料类别id
                fMaterialid: data.fMaterialid, //材料id
                fSpecification: data.fSpecification, //规格型号
                fSupplierid: data.fSupplierid //供应商id
            }
            // this.tableObj[index]['locationData'] = []
            // this.loading = true;
            // this.tableObj[index]['locationData'] = []
            getCFDdByEntryledger(obj)
                .then((res) => {
                    if (res.errcode == 0) {
         
                        this.tableObj[index]['locationData'] = res.data


                        var total = 0
                        this.tableObj.map(item => {
                            if (item.fClyl != null && item.fClyl != "/" && item.fClyl != "") {
                                total += (item.fClyl - 0)
                            } else {
                                total += 0
                            }
                        })
                        this.tableObj[0].fDwtjl = total;
                        this.$forceUpdate()
                        this.tableData = this.tableObj;
                        console.log(this.tableData);


                    } else {

                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
            // .catch((error) => {
            //     this.$message.error("列表数据 接口出错1", error.msg);
            //     this.loading = false;
            // });


        },
        // 计算含水量
        countWater(e, index, fLlyl) {
            if (fLlyl != null || fLlyl != "/" || fLlyl != "") {
                var sum = fLlyl * (e / 100)
                this.tableData[index].fHsnum = sum.toFixed(2)
                this.tableData[index].fClyl = (fLlyl - 0) + sum
            } else {
                this.tableData[index].fHsnum = "/"
                this.tableData[index].fClyl = "/"


            }
            var num = 0;
            this.tableData.map((item, index) => {
                if (index <= 5) {
                    if (item.fHsnum != null || item.fHsnum != "/" || item.fHsnum != "") {
                        num += (item.fHsnum - 0)

                    } else {
                        num += 0

                    }
                }

            })

            this.tableData[6].fHsnum = (-num).toFixed(2)
            this.tableData[6].fClyl = (this.tableData[6].fLlyl - 0) + (this.tableData[6].fHsnum - 0)
            var total = 0
            this.tableData.map(item => {
                if (item.fClyl != null && item.fClyl != "/" && item.fClyl != "") {
                    total += (item.fClyl - 0)
                } else {
                    total += 0
                }
            })
            this.tableData[0].fDwtjl = total.toFixed(2)
        },

        // 详情
        getDetail(obj) {

            // var that = this
            getSGPHBByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data.yjQmpGySgphb


                    this.formData = data
                    // var status = obj.fId == '' ? null : 0

                    this.tableObj = data.YjQmpGySgphbmaterial
                    this.formData.fSgunitid = this.userInfo.companyId //标段id
                    this.formData.fSgunitname = this.userInfo.companyName //标段名称
                    this.formData.fJlunitid = this.userInfo.jlCompanyId //项目id
                    this.formData.fJlunitname = this.userInfo.jlCompanyName //项目名称

                    if (obj.fId == '') {
                        this.formData.fCompanyid = this.userInfo.companyId //标段id
                        this.formData.fCompanyname = this.userInfo.companyName //标段名称
                        this.formData.fFileurl = res.data.yjQmpGySgphb.fFileurl
                        this.formData.fSgphbdates = this.common.day(0)
                    } else {
                        this.formData = res.data.yjQmpGySgphb

                        this.formData.fDesignedslump = data.fDesignedslump.split(",")
                        this.tableObj = res.data.yjQmpGySgphb.yjQmpGySgphbmaterials
                        this.tablesgData = res.data.yjQmpGyLlphbrawmaterials
                        this.fileList = res.data.fFileurllist
                        this.tableObj.map((item, index) => {
                            this.getCFDdByEntryledger(item, index)
                        })


                    }

                }
            });
        },
        getLLPHBByFids(id) {
            var obj = {
                fId: id, //左侧选中组织机构id
            }
            this.loading = true;
            var that = this;
            getLLPHBByFids(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        res.data.YjQmpGySgphbmaterial.map((item, index) => {
                            that.getCFDdByEntryledger(item, index)
                        })

                        this.tableObj = res.data.YjQmpGySgphbmaterial
                        this.tablesgData = res.data.YjQmpGyLlphbrawmaterial


                    } else {
                        this.$message.error("施工配合比数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("施工配合比数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        yj_searchForm() {
            var obj = {
                fCompanyid: this.userInfo.companyId, //左侧选中组织机构id
                keyType: "", //上方查询条件材料种类
                // taskNumber: data.taskNumber, //上方查询条件进场编号
                page: 1, //当前页码
                pageSize: 500, //每页显示条数
                userId: this.userInfo.userId, //查询待办或已办时传 列表页查询不传
                modelType: 2, //0-待办 1-已办 传2 表示列表页查询
            }
            this.loading = true;
            var that = this;
            getyjQmpGyLlphbPageLists(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        this.formObj[0].options = []
                        res.data.pageInfo.list.map(item => {
                            this.formObj[0].options.push({
                                label: item.fLlphbnumber,
                                value: item
                            })
                        })

                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        yj_saveForm() {


            // yjQmpGyLlphbrawmaterial

            this.userInfo = JSON.parse(sessionStorage.getItem("monitorBack-userInfo"));
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            this.formData.fReportid = this.userInfo.userId; //当前登录人id
            this.formData.fReportname = this.userInfo.realName; //当前登录人名称

            this.formData.fProjectid = this.userInfo.projectId //项目id
            this.formData.fProjectname = this.userInfo.projectName //项目名称

            this.formData.yjQmpGySgphbmaterials = this.tableData
            if (this.$refs.publicForm.submitForm()) {
                this.formData.fDesignedslump = this.formData.fDesignedslump.join(",")
                this.saveForm(this.formData, 0);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(obj) {
            // var url;

            // if (status == 0) {
            //     url = saveOutsourcednon //闭合
            // } else if (status == 1) {
            //     url = checkOutsourcednon1 //一级审核
            // } else if (status == 2) {
            //     url = checkOutsourcednon2 //二级审核
            // }
            this.isLock = true
            saveyjQmpGySgphb(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });
        },
    },
};